package com.dc.controller;

import com.dc.base.aop.AopOperation;
import com.dc.base.baseController.BaseController;
import com.dc.base.em.ErrorMessageEnum;
import com.dc.base.em.RoleMenuEnum;
import com.dc.base.vo.BaseModel;
import com.dc.base.vo.BusinessException;
import com.dc.model.Inventory;
import com.dc.model.User;
import com.dc.service.InventoryService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Api(value="库存模块")
@RequestMapping("/inventory")
@Controller
public class InventoryController extends BaseController {
    @Autowired
    InventoryService inventoryService;

    // 分页查询库存信息
    // 新增日志信息
    @AopOperation(desc = "分页查询库存信息",type = "查询",menu = RoleMenuEnum.NO_203)
    @ApiOperation(value="分页查询库存信息",httpMethod = "GET",notes = "查询")
    @RequestMapping(value = "/selectPageInventory", method = RequestMethod.GET)
    @ResponseBody
    public BaseModel selectPageInventory(
            @RequestParam(value="curr_page",defaultValue = "1") int curr_page,
            @RequestParam(value="page_size",defaultValue = "10") int page_size,
            BaseModel baseModel,
            @RequestParam(value="inventory_id",required = false) Integer inventory_id,
            @RequestParam(value="flower_name",required = false)String flower_name){
        return  inventoryService.selectPageInventory(curr_page,page_size,baseModel,inventory_id,flower_name);

    }

    // 操作日志注解，新增操作日志
    @AopOperation(desc = "新增库存",type="新增",menu = RoleMenuEnum.NO_203)
    // 新增库存信息
    @ApiOperation(value="库存新增",httpMethod = "POST",notes="新增")
    @RequestMapping(value="/addInventory", method = RequestMethod.POST)
    @ResponseBody
    public BaseModel addInventory(Inventory inventory, HttpServletRequest request, BaseModel baseModel)throws Exception{
        /* 获取session,判断是否登录 */
        HttpSession session=request.getSession();
        User currUser=(User)session.getAttribute("currentUser");
        if(currUser==null) {
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        inventory.setUpdate_time(new Date());
        return inventoryService.addInventory(inventory,baseModel);
    }

    // 操作日志注解，新增操作日志
    @AopOperation(desc = "修改库存信息",type="修改",menu = RoleMenuEnum.NO_203)
    // 修改库存信息
    @ApiOperation(value="库存修改",httpMethod = "POST",notes="修改")
    @RequestMapping(value="/updateInventory", method = RequestMethod.POST)
    @ResponseBody

    public BaseModel updateInventory(Inventory inventory,BaseModel baseModel,HttpServletRequest request)throws Exception {
        inventory.setUpdate_time(new Date());
        // 获取登录信息
        HttpSession session=request.getSession();
        // 记住要用User
        User currentUser=(User) session.getAttribute("currentUser");
        if(currentUser==null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return inventoryService.updateInventory(inventory,baseModel);
    }

    // 根据id查询库存信息
    @ApiOperation(value="库存查询",httpMethod = "GET",notes="查询")
    @RequestMapping(value="/selectInventoryById", method = RequestMethod.GET)
    @ResponseBody
    public BaseModel selectInventoryById(@RequestParam("inventory_id")Integer inventory_id,
                                      BaseModel baseModel,
                                      HttpServletRequest request)throws Exception{
        return inventoryService.selectInventoryById(inventory_id,baseModel);
    }

    // 操作日志注解，新增操作日志
//    @AopOperation(desc = "删除库存",type="删除",menu = RoleMenuEnum.NO_203)
    // 删除库存信息(修改)
//    @ApiOperation(value="库存删除",httpMethod = "POST",notes="删除")
//    @RequestMapping(value="/deleteInventory/{ids}", method = RequestMethod.POST)
//    @ResponseBody
//    public BaseModel deleteInventory(@PathVariable(value="ids") String ids,
//                                  BaseModel baseModel,
//                                  HttpServletRequest request)throws Exception{
//        HttpSession session=request.getSession();
//        User currentUser = (User)session.getAttribute("currentUser");
//        if(currentUser==null){
//            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
//        }
//        return inventoryService.deleteInventory(ids,baseModel);
//    }
}
